package com.yhp.servlet;

import com.yhp.bean.Grade;
import com.yhp.bean.Student;
import com.yhp.service.impl.GradeServiceImpl;
import com.yhp.service.impl.StudentService;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/servlets")
public class StudentServlets extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取头信息进行分类操作
        String service = req.getParameter("service");
        //不同请求进行分类
        // 一，/Educational/student/addstu
        if ("addstu".equals(service)) {
            add(req,resp);
        }
        // 二， /Educational/student/finddelid
        else if ("finddelid".equals(service)) {
           did(req,resp);
        }
        // 三， /Educational/student/getGradeList
        else if  ("getGradeList".equals(service)) {
           getGid(req,resp);
        }
        // /Educational/student/findbyid
        else if  ("findbyid".equals(service)) {
           fid(req,resp);
        }

        //  /Educational/student/updatastu
        else if  ("updatastu".equals(service)) {
           updata(req,resp);
        }
        // /Educational/student/getStudentList
        else{
            getstu(req,resp);
        }
    }
    public void getGid(HttpServletRequest req, HttpServletResponse resp){
        List<Grade> grades = new GradeServiceImpl().getList();
        req.setAttribute("grades",grades);
        try {
            req.getRequestDispatcher("add.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void updata(HttpServletRequest req, HttpServletResponse resp){
        //1.获取数据
        String stuid = req.getParameter("stuid");
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idnumber");
        String profession = req.getParameter("profession");
        //2.调取service，更新数据
        Student student = new Student();
        student.setStuid(Integer.parseInt(stuid));
        student.setStuno(stuno);
        student.setStuname(stuname);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(Long.parseLong(phone));
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdnumber(Long.parseLong(idnumber));
        student.setProfession(profession);
        student.setIntroduction(req.getParameter("introduction"));
        int zt = StudentService.upDate(student);
        //3.跳转页面，反馈修改结果
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (zt>0){
            writer.println("<script>alert('修改成功！');location.href='/Educational/student/servlets?service=getStudentList';</script>");
        }else {
            writer.println("<script>alert('修改失败！');location.href='/Educational/student/servlets?service=findbyid&sid="+stuid+"';</script>");
        }
    }
    public void getstu(HttpServletRequest req, HttpServletResponse resp){
        //1.获取参数
        //1.1模糊查找
        String name = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        sex = sex==null? "-1":sex;//防止空异常
        //1.2分页数据 linit 开始位置，显示条数
        String pageindex = req.getParameter("pageIndex");
        int index = pageindex==null? 1:Integer.parseInt(pageindex);//如果为空则默认为第一页
        //2.调取service
        //2.1获取学生信息
        List<Student> students =
                StudentService.getstudent(name,stuno,Integer.parseInt(sex),index,5);
        //2.2获取总页数 = 总条数%显示数+1
        int total = StudentService.total(name,stuno,Integer.parseInt(sex));
        int totalPage = total%5>0?total/5+1:total/5;
        //3.存入数据跳转页面
        //3.1存入搜索记录
        req.setAttribute("name",name);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //3.2存入当前页码数
        req.setAttribute("index",index);
        req.setAttribute("size",5);
        req.setAttribute("total",total);
        req.setAttribute("totalPage",totalPage);
        //3.3存入学生信息
        req.getSession().setAttribute("students",students);
        try {
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void fid(HttpServletRequest req, HttpServletResponse resp){
        //1.获取id值
        int id = Integer.parseInt(req.getParameter("sid"));
        //2.调取service。通过id进行查询
        //2.1获取student信息
        Student student = StudentService.findStudent(id);
        req.setAttribute("student",student);
        //2.2获取年级信息
        List<Grade> grades = new GradeServiceImpl().getList();
        req.setAttribute("grades",grades);
        //3.跳转页面
        try {
            req.getRequestDispatcher("edit.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void did(HttpServletRequest req, HttpServletResponse resp){
        //1.获取id值
        int id = Integer.parseInt(req.getParameter("sid"));
        //2.调取service。通过id进删除
        int zt = StudentService.deletStudent(id);
        //3.跳转页面刷新
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (zt>0){
            writer.println("<script>alert('删除成功！');location.href='/Educational/student/servlets?service=getStudentList';</script>");
        }else {
            writer.println("<script>alert('删除失败！');location.href='/Educational/student/servlets?service=getStudentList';</script>");
        }
    }
    public void add(HttpServletRequest req, HttpServletResponse resp){
        //1.获取数据乱码处理
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idnumber");
        String introduction = req.getParameter("Introduction");
        String profession = req.getParameter("profession");
        //2.调取service，存入数据
        Student student = new Student();
        student.setStuno(stuno);
        student.setStuname(stuname);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(Long.parseLong(phone));
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdnumber(Long.parseLong(idnumber));
        student.setIntroduction(introduction);
        student.setProfession(profession);
        int zt = StudentService.addStudent(student);
        //3.跳转页面刷新
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (zt>0){
            writer.println("<script>alert('新增成功！');location.href='/Educational/student/servlets?service=getStudentList';</script>");
        }else {
            writer.println("<script>alert('新增失败！');location.href='/Educational/student/servlets?service=getGradeList';</script>");
        }
    }
}
